package br.gendi.rcp.preferences;

import java.util.Properties;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

import br.gendi.core.Configuracoes;
import br.gendi.rcp.Activator;

public class ProxyPreferencePage
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {

	StringFieldEditor f1, f2, f3, f4;
	BooleanFieldEditor g1;

	private static String strue = "true";
	private static String sfalse = "false";
	
	public ProxyPreferencePage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		//setDescription("A demonstration of a preference page implementation");
	}

	public void createFieldEditors() {

		g1 = new BooleanFieldEditor(PreferenceConstants.P_CHOICE, "Utilizar Proxy", getFieldEditorParent());
		
		addField(g1);

		f1 = new StringFieldEditor(PreferenceConstants.P_STRING, "&Endereço host proxy:", getFieldEditorParent());
		f2 = new StringFieldEditor(PreferenceConstants.P_STRING, "&Porta do proxy :", getFieldEditorParent());
		f3 = new StringFieldEditor(PreferenceConstants.P_STRING, "&Usuário do proxy:", getFieldEditorParent());
		f4 = new StringFieldEditor(PreferenceConstants.P_STRING, "&Senha do proxy:", getFieldEditorParent());
	
		addField(f1);
		addField(f2);
		addField(f3);
		addField(f4);
	}

	public void init(IWorkbench workbench) {

	}

	protected void checkState() {
		super.checkState();
		if (g1.getBooleanValue() == false) {
			f1.setEnabled(false, getFieldEditorParent());
			f2.setEnabled(false, getFieldEditorParent());
			f3.setEnabled(false, getFieldEditorParent());
			f4.setEnabled(false, getFieldEditorParent());
			
		} else {
			f1.setEnabled(true, getFieldEditorParent());
			f2.setEnabled(true, getFieldEditorParent());
			f3.setEnabled(true, getFieldEditorParent());
			f4.setEnabled(true, getFieldEditorParent());
		}

		if(f1.getStringValue()!= null && !f1.getStringValue().equals("")){
			setErrorMessage(null);
			setValid(true);
		}else{
			setErrorMessage("Endereço de proxy não pode ser vazio.");
			setValid(false);
		}

		if(f2.getStringValue()!= null && !f2.getStringValue().equals("")){
			setErrorMessage(null);
			setValid(true);
		}else{
			setErrorMessage("Porta do proxy não pode ser vazia.");
			setValid(false);
		}
	}

	public void propertyChange(PropertyChangeEvent event) {
		super.propertyChange(event);
		if (event.getProperty().equals(FieldEditor.VALUE)) {
			checkState();
		}        
	}

	public boolean performOk() {
		Configuracoes.salvarProxy(g1.getBooleanValue(), f1.getStringValue(), f2.getStringValue(), f3.getStringValue(), f4.getStringValue());
		return super.performOk();
	}
	
	protected void initialize() {
		super.initialize();
		Properties p = Configuracoes.loadProxy();
		ScopedPreferenceStore s = (ScopedPreferenceStore) g1.getPreferenceStore();
		if (p.getProperty("ativo").equals("true")) {
			s.setValue(PreferenceConstants.P_CHOICE, true);
		} else {
			s.setValue(PreferenceConstants.P_CHOICE, false);
		}
		
		f1.setStringValue(p.getProperty("host"));
		f2.setStringValue(p.getProperty("port"));
		f3.setStringValue(p.getProperty("user"));
		f4.setStringValue(p.getProperty("password"));
	}

}